package com.msb.servicemap.remote;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.msb.commons.constant.AmapConstant;
import com.msb.commons.response.DirectionResponse;
import com.msb.commons.response.FalconServiceResponse;
import com.msb.commons.response.ResponseResult;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: chshh
 * @version: 1.0
 */
@Service
public class FalconServiceClient {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${amap.key}")
    private String amapKey;

    public ResponseResult add(String name){
        //组装请求调用url
        // https://tsapi.amap.com/v1/track/service/add
        // ?key=0e18e92d4fd4b3d09eb13666c11b8004
        // &name=%E7%BD%91%E7%BA%A6%E8%BD%A6service
        StringBuilder urlBuilder = new StringBuilder();

        urlBuilder.append(AmapConstant.FALCON_SERVICE_ADD_URL);
        urlBuilder.append("?");
        urlBuilder.append("key="+amapKey);
        urlBuilder.append("&");
        urlBuilder.append("name="+name);

        //调用高德接口
        ResponseEntity<String> entity = restTemplate.postForEntity(urlBuilder.toString(),null, String.class);

        //解析接口
        String body = entity.getBody();
        JSONObject jsonObject = JSONObject.fromObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        String sid = data.getString("sid");
        FalconServiceResponse falconServiceResponse = new FalconServiceResponse();
        falconServiceResponse.setSid(sid);

        return ResponseResult.success(falconServiceResponse);

    }
}
